package user;

import com.liCheng.entity.User;
import com.liCheng.service.user.UserService;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserTest {
    private static Logger logger=Logger.getLogger(UserTest.class);

    /**
     * 模糊查询
     */
    @Test
    public void UserMapperTest(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        UserService userService=(UserService) ctx.getBean("userService");
        User userCondition=new User();
        userCondition.setUserName("赵");
        userCondition.setUserRole(3);
        List<User> userList=new ArrayList<User>();
        userList=userService.findUsersWithConditions(userCondition);
        for (User userResult : userList) {
            logger.debug("testGetUserList userCode:"+ userResult.getUserCode()+" and userName:"
                        +userResult.getUserName()+" and userRole:"
                        +userResult.getUserRole()+" and userRoleName:"
                        +userResult.getUserRoleName()+" and address:"
                        +userResult.getAddress());
        }
    }

    /**
     * 添加
     */
    @Test
    public void UserMapperAdd(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        UserService userService=(UserService) ctx.getBean("userService");
        User user=new User();
        user.setUserCode("asd");
        user.setUserName("赵三");
        user.setUserPassword("123456");
        user.setGender(1);
        user.setBirthday(new Date());
        boolean result=userService.addNewUser(user);
        if (result==true){
            logger.debug("testAdd result:"+"添加成功");
        }

    }

    /**
     * 修改
     */
    @Test
    public void UserMapperUpdate(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        UserService userService=(UserService) ctx.getBean("userService");
        User user=new User();
        user.setId(19);
        user.setUserCode("abc");
        user.setUserName("赵四");
        user.setUserPassword("1234567");
        int result=userService.updateUser(user);
        if (result>0){
            logger.debug("testAdd result:"+"修改成功");
        }

    }

    /**
     * 删除
     */
    @Test
    public void UserMapperDel(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
        UserService userService=(UserService) ctx.getBean("userService");
        User user=new User();
        user.setId(19);
        int result=userService.delUser(user);
        if (result>0){
            logger.debug("testAdd result:"+"删除成功");
        }

    }
}
